/**
 * 
 */
package com.youxin.cpcenter.utils;

import com.youxin.busfoundation.bean.BusinessMessages;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.common.BusinessDataException;
import com.youxin.busfoundation.common.BusinessException;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.busfoundation.enums.EnterpriseIdentifyStatus;
import com.youxin.busfoundation.enums.EnterpriseManagerType;
import com.youxin.busfoundation.enums.EnterprisePassStatus;
import com.youxin.cpcenter.bean.member.QueryMemberInfo;
import com.youxin.cpcenter.dao.TbEntMemberIdentifyMapper;
import com.youxin.cpcenter.dao.TbEntMemberInfoMapper;
import com.youxin.cpcenter.dao.TbMemberInfoMapper;
import com.youxin.cpcenter.model.TbEntMemberIdentify;
import com.youxin.cpcenter.model.TbEntMemberInfo;
import com.youxin.cpcenter.model.TbMemberInfo;
import com.youxin.foundation.data.Tuple;

import net.bytebuddy.asm.Advice.This;

/**
 * @description 用户权限控制工具类
 * @author yingde.cao
 * @date 2017年12月10日
 * @version 1.0
 */
public class MemberRightsUtils {

	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(MemberRightsUtils.class);

	/**
	 * 查询会员
	 * 
	 * @param mapper
	 * @param memberId
	 * @return
	 */
	public static TbMemberInfo isMemberInfo(TbMemberInfoMapper mapper, long memberId) {
		TbMemberInfo memeberInfo = mapper.selectByPrimaryKey(memberId);
		if (memeberInfo == null) {
			throw new BusinessException("无法查询到会员信息");
		}
		return memeberInfo;
	}

	/**
	 * 
	 * @description 是否普通会员
	 * @param tbMemberInfoMapper
	 * @return TbMemberInfo
	 */
	public static TbMemberInfo isMemberInfo(TbMemberInfoMapper tbMemberInfoMapper) {
		UserInfo userInfo = LoginAuth.getUserInfo();
		return isMemberInfo(tbMemberInfoMapper, userInfo);
	}

	public static TbMemberInfo isMemberInfo(TbMemberInfoMapper tbMemberInfoMapper, UserInfo userInfo) {
		if (userInfo == null) {
			throw new BusinessDataException(BusinessMessages.LOGIN_INFO);
		}
		TbMemberInfo memberInfo = tbMemberInfoMapper.selectByPrimaryKey(userInfo.getId());
		if (memberInfo == null) {
			throw new BusinessException("无法查询到会员信息");
		}
		return memberInfo;
	}

	/**
	 * @description 是否企业认证会员
	 * @param tbMemberInfoMapper
	 * @return TbMemberInfo
	 */
	public static TbMemberInfo isMemberIdentify(TbMemberInfoMapper tbMemberInfoMapper) {
		UserInfo userInfo = LoginAuth.getUserInfo();
		TbMemberInfo memberInfo = isMemberInfo(tbMemberInfoMapper, userInfo);
		if (!userInfo.isMemberIdentity()) {
			throw new BusinessException("成员[" + memberInfo.getUsername() + "]不是企业认证成员");
		}
		return memberInfo;
	}

	public static TbMemberInfo isMemberIdentifyAndPassed(TbMemberInfoMapper tbMemberInfoMapper, long memberId) {
		TbMemberInfo memberInfo = isMemberInfo(tbMemberInfoMapper, memberId);
		if (!memberInfo.isEntIdentity()) {
			throw new BusinessException("成员[" + memberInfo.getUsername() + "]不是企业会员");
		}
		return memberInfo;
	}

	public static TbMemberInfo isMemberIdentifyAndPassed(TbMemberInfoMapper tbMemberInfoMapper,
			TbEntMemberInfoMapper tbEntMemberInfoMapper, long memberId, String companyUid) {
		TbMemberInfo memberInfo = isMemberIdentifyAndPassed(tbMemberInfoMapper, memberId);
		QueryMemberInfo bean = new QueryMemberInfo();
		bean.setMemberId(memberId);
		bean.setCompanyUid(companyUid);
		TbEntMemberInfo entMemberInfo = tbEntMemberInfoMapper.selectByMemberIdAndCompanyUid(bean);
		if (entMemberInfo == null
				|| entMemberInfo.getEnterprisePass().shortValue() != EnterprisePassStatus.Pass.getValue()) {
			throw new BusinessException("成员[" + memberInfo.getUsername() + "]不是企业会员");
		}
		return memberInfo;
	}

	public static Tuple<TbMemberInfo, TbEntMemberInfo> isMemberIdentify(TbMemberInfoMapper tbMemberInfoMapper,
			TbEntMemberInfoMapper tbEntMemberInfoMapper, long memberId, String companyUid) {
		TbMemberInfo memberInfo = isMemberIdentifyAndPassed(tbMemberInfoMapper, memberId);
		QueryMemberInfo bean = new QueryMemberInfo();
		bean.setMemberId(memberId);
		bean.setCompanyUid(companyUid);
		TbEntMemberInfo entMemberInfo = tbEntMemberInfoMapper.selectByMemberIdAndCompanyUid(bean);
		if (entMemberInfo == null || entMemberInfo.getEnterpriseIdentyStatus()
				.shortValue() != EnterpriseIdentifyStatus.Authenticated.getValue()) {
			throw new BusinessException("成员[" + memberInfo.getUsername() + "]不是企业认证成员");
		}
		return new Tuple<>(memberInfo, entMemberInfo);
	}

	public static TbEntMemberInfo isEntMemberIdentifyAndPassed(TbMemberInfoMapper tbMemberInfoMapper,
			TbEntMemberInfoMapper tbEntMemberInfoMapper, long memberId, String companyUid) {
		TbMemberInfo memberInfo = isMemberIdentifyAndPassed(tbMemberInfoMapper, memberId);
		QueryMemberInfo bean = new QueryMemberInfo();
		bean.setMemberId(memberId);
		bean.setCompanyUid(companyUid);
		TbEntMemberInfo entMemberInfo = tbEntMemberInfoMapper.selectByMemberIdAndCompanyUid(bean);
		if (entMemberInfo == null
				|| entMemberInfo.getEnterprisePass().shortValue() != EnterprisePassStatus.Pass.getValue()) {
			throw new BusinessException("成员[" + memberInfo.getUsername() + "]不是企业会员");
		}
		return entMemberInfo;
	}

	/**
	 * @description 是否企业认证会员并且开通企业会员
	 * @param tbMemberInfoMapper
	 * @return TbMemberInfo
	 */
	public static TbMemberInfo isMemberIdentifyAndPassed(TbMemberInfoMapper tbMemberInfoMapper) {
		UserInfo userInfo = LoginAuth.getUserInfo();
		TbMemberInfo memberInfo = isMemberInfo(tbMemberInfoMapper, userInfo);
		if (!userInfo.isMemberIdentityAndPassed()) {
			throw new BusinessException("成员[" + memberInfo.getUsername() + "]不是企业会员");
		}
		return memberInfo;
	}

	/**
	 * @description 是否企业管理员
	 * @param tbMemberInfoMapper
	 * @return TbMemberInfo
	 */
	public static TbMemberInfo isAdmin(TbMemberInfoMapper tbMemberInfoMapper) {
		UserInfo userInfo = LoginAuth.getUserInfo();
		TbMemberInfo memberInfo = isMemberInfo(tbMemberInfoMapper, userInfo);
		if (!userInfo.isAdmin())// ||!memberInfo.isEntIdentity()
		{
			logger.error("不是企业管理员:"+userInfo.toString());
			throw new BusinessException("不是企业管理员");
		}
		return memberInfo;
	}

	public static TbEntMemberIdentify isAdminIdentity(TbEntMemberIdentifyMapper tbEntMemberIdentifyMapper,
			long entIdentityId, long memberId) {
		TbEntMemberIdentify tbEntMemberIdentify = tbEntMemberIdentifyMapper.selectByPrimaryKey(entIdentityId);
		if (tbEntMemberIdentify == null) {
			throw new BusinessException("当前用户不是企业管理员,没有找到关联企业.");
		}
		if (tbEntMemberIdentify.getMemberId().longValue() != memberId) {
			throw new BusinessException("当前用户不是企业管理员,没有找到关联企业.");
		}
		if (tbEntMemberIdentify.getStatus() != EnterpriseIdentifyStatus.Authenticated.getValue()) {
			throw new BusinessException("当前企业没有认证.");
		}
		return tbEntMemberIdentify;
	}

}
